﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// TMS运价获取
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSFreightController : ControllerBase
    {
        private readonly ILogger<TMSFreightRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public TMSFreightController(ILogger<TMSFreightRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");

        }

        /// <summary>
        /// TMS运价获取
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTMSFreight([FromBody] TMSFreightRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            TMSFreightRequestDto result = new TMSFreightRequestDto();
            TMSFreightResponseDto re = new TMSFreightResponseDto();
            string dispatch_no = strparm.dispatch_no;
            string carrierid_code = strparm.carrierid_code;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);
            decimal yprice = 0;

            try
            {

                var row = strparm.rows;
                foreach (var item in row)
                {
                    string startaddress = item.startaddress;
                    string targetaddress = item.targetaddress;
                    string cartypecode = item.cartypecode;
                    decimal materialamount = item.materialamount;

                    decimal price = 0;
                    strSql = new StringBuilder();
                    strSql.Append(@"select * from tms_base_freight where start_address='"+ startaddress + "' and 	target_address='"+ targetaddress + "' and " +
                        "	cartypecode='" + cartypecode + "' and  carrierid_code='"+ carrierid_code + "'  and  (effectivedate<=CURDATE() and  expirydate>=CURDATE())     order by creation_date  desc  LIMIT 1");
                    DataTable dt_price = DB.Query(strSql.ToString(), con);
                    if (dt_price.Rows.Count > 0)
                    {
                        price = DB.ToDecimal(dt_price.Rows[0]["freightprice"].ToString());

                        if (yprice==0)
                        {
                            yprice = price;
                        }
                        else
                        {
                            if (yprice>= price)
                            {
                                yprice = price;
                            }
                        }
                    }                  
                }

                if (yprice==0)
                {
                    strMsg = "价格获取失败或价格失效，检查运价档案是否已经录入相关数据";
                }

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
                re.price = yprice;
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
